<?php

/**
 * @author jeiry
 * @copyright 2010
 * @pic upload class
 */

class uploadpic{
	var $pic_link;
	var $pic;
	function __construct($pic_link,$pic){
		$this->pic_link=$pic_link;
		$this->pic=$pic;
		}

	function pic_name(){
		$pic_link=$this->pic_link;
		$uploadpicfile=$this->pic;
		if (($uploadpicfile["type"] == "image/gif")||($uploadpicfile["type"] == "image/jpeg")||($uploadpicfile["type"] == "image/pjpeg")||($uploadpicfile["type"] == "image/png")){
	 	 if ($uploadpicfile["error"] == 0)
   			 {
				 if($uploadpicfile["type"] == "image/gif"){$picformat=gif;}
				 elseif($uploadpicfile["type"] == "image/jpeg" || $uploadpicfile["type"] == "image/pjpeg"){$picformat=jpg;}
				 elseif($uploadpicfile["type"] == "image/png"){$picformat=png;}
			$tmp_name = $uploadpicfile["tmp_name"];
        	$name1 = filetime.'.'.$picformat;
			move_uploaded_file($tmp_name, $pic_link."/$name1");//移动原图到指定目录
			
			return filetime.'.'.$picformat;
			//exit;

		 }}
			else{echo '上传失败，格式必须是 gif、jpen、pjpeg、png'; exit();}
		}
	
	function pic_folder(){
		$nname1 = $this->pic_name();
		return substr($nname1,0,8);
		}
		
	function pic_check(){
		$pic_link=$this->pic_link;
		$nname1 = $this->pic_name();
		
		
		$image = $pic_link."/$nname1"; // 得到原图
			
			
		list($width, $height, $type, $attr) = getimagesize($image);
		//echo "width: $width height: $height type: $type attr: $attr <br />";
		$uploadfilesize = filesize($image) / 1000;
		//echo "$uploadfilesize kb ||";
//获取图片信息
			function youcandoit($condition){
				unlink($condition);
				echo '<p>&nbsp;</p><a href="javascript:history.go(-1);">返回上一页</a>';
				exit();
			}
			//删除更上传的原图
		if($uploadfilesize>1000){echo "文件必需少于1Mb"; youcandoit($condition=$image);}
		elseif($type==1 and $type==2 and $type==3){echo '格式只能为JPG , GIF , PNG'; youcandoit($condition=$image);}
		elseif($width>1000){ echo '文件宽度不能大于1000像素'; youcandoit($condition=$image);}
		//检查图片
$picx=($width-$height)/2;
$picy=($height-$width)/2;
//截取图片正中间
if($width>$height){$tempsize=$height; $picy=0; $newpicsizeheight=$height;}
	elseif($width<$height){$tempsize=$width; $picx=0; $newpicsizeheight=$width;}
		elseif($width==$height){$tempsize=$width; $picy=0; $picx=0; $newpicsizeheight=$width;}
//分析图片长宽
$temppic = imagecreatetruecolor($tempsize, $tempsize);
if($type==1){$source = imagecreatefromgif($image);}
elseif($type==2){$source = imagecreatefromjpeg($image);}
elseif($type==3){$source = imagecreatefrompng($image);}
//创建三种格式的图片

if(parameters($con1=5)==0){
imagecopyresized($temppic, $source, 0, 0, $picx, $picy, $width, $height, $width, $height);
//读取并生成图像
$newwidth=200;
$newheight=200;
//新图片尺寸
$thumb = imagecreatetruecolor($newwidth, $newheight);
imagecopyresized($thumb,$temppic,0,0,0,0,$newwidth,$newheight,$newpicsizeheight,$newpicsizeheight);
$filename1 = basename($image);  
}else{
	
	$wnh = explode(',',parameters($con1=6));
$nwidth=$wnh[1];
$nheight=$wnh[0];
$thumb = imagecreatetruecolor($nwidth, $nheight);
imagecopyresized($thumb,$source,0,0,0,0,$nwidth,$nheight,$width,$height);
$filename1 = basename($image);  

//生成图片文件
	}
imagejpeg($thumb,$pic_link."/spic/$nname1", 100);

//生成图片文件

		}
		
	function createfolder() 
	{ 
	$pic_link=$this->pic_link;
	if (!file_exists($pic_link)) 
	{ 
	mkdir($pic_link, 0777); 
		} 
	if (!file_exists($pic_link.'/spic')) 
	{ 
	mkdir($pic_link.'/spic', 0777); 
		} 
		//$this->article_path()
		$this->pic_check();
		} 
	
		}


?>